﻿using System;
using System.Configuration;
using System.Net.Mail;

namespace FacialClub.Helpers
{
    /// <summary>
    /// Mail Helpers
    /// </summary>
    public class MailHelpers
    {
        /// <summary>
        /// Sends an mail message
        /// </summary>
        /// <param name="strSenderName"></param>
        /// <param name="strSenderEmail"></param>
        /// <param name="strReceiverName"></param>
        /// <param name="strReceiverEmail"></param>
        /// <param name="strCCName"></param>
        /// <param name="strCCEmail"></param>
        /// <param name="strSubject"></param>
        /// <param name="strBody"></param>
        public static void SendMailMessage(string strSenderName,string strSenderEmail,string strReceiverName,string strReceiverEmail,string strCCName,string strCCEmail,string strSubject,string strBody)
        {
            try
            {
                // Instantiate a new instance of MailMessage
                var mail = new MailMessage();
                // Set the sender address of the mail message
                mail.From = !string.IsNullOrEmpty(strSenderName) ? new MailAddress(strSenderEmail, strSenderName) : new MailAddress(strSenderEmail);

                // Set the recepient address of the mail message
                mail.To.Add(!string.IsNullOrEmpty(strReceiverName) ? new MailAddress(strReceiverEmail, strReceiverName) : new MailAddress(strReceiverEmail));

                // Check if the cc value is null or an empty value
                if (!string.IsNullOrEmpty(strCCEmail))
                {
                    // Set the CC address of the mail message
                    mail.CC.Add(new MailAddress(strCCEmail, strCCName));
                }

                // Set the subject of the mail message
                mail.Subject = strSubject;

                // Set the body of the mail message
                mail.Body = strBody.Replace("{ReceiverEmail}", strReceiverEmail).Replace("{ReceiverName}", strReceiverName).Replace("{SenderEmail}", strSenderEmail).Replace("{SenderName}", strSenderName);

                // Set the format of the mail message body as HTML
                mail.IsBodyHtml = true;

                // Set the priority of the mail message to normal
                mail.Priority = MailPriority.Normal;

                // Instantiate a new instance of SmtpClient
                var mSmtpClient = new SmtpClient("mail.corp.salu.com");

                // Send the mail message
                mSmtpClient.Send(mail);
            }
            catch(Exception)
            {
                
            }
        }

        /// <summary>
        /// Send Email with network credentials
        /// </summary>
        /// <param name="strSenderName"></param>
        /// <param name="strSenderEmail"></param>
        /// <param name="strReceiverName"></param>
        /// <param name="strReceiverEmail"></param>
        /// <param name="strCCName"></param>
        /// <param name="strCCEmail"></param>
        /// <param name="strSubject"></param>
        /// <param name="strBody"></param>
        public static void SendMailMessageWithCredentials(string strSenderName,string strSenderEmail,string strReceiverName,string strReceiverEmail,string strCCName,string strCCEmail,string strSubject,string strBody)
        {
            try
            {
                // Instantiate a new instance of MailMessage
                var mail = new MailMessage();
                // Set the sender address of the mail message
                mail.From = !string.IsNullOrEmpty(strSenderName) ? new MailAddress(strSenderEmail, strSenderName) : new MailAddress(strSenderEmail);

                // Set the recepient address of the mail message
                mail.To.Add(!string.IsNullOrEmpty(strReceiverName) ? new MailAddress(strReceiverEmail, strReceiverName) : new MailAddress(strReceiverEmail));

                // Check if the cc value is null or an empty value
                if (!string.IsNullOrEmpty(strCCEmail))
                {
                    // Set the CC address of the mail message
                    mail.CC.Add(new MailAddress(strCCEmail, strCCName));
                }

                // Set the subject of the mail message
                mail.Subject = strSubject;

                // Set the body of the mail message
                mail.Body = strBody.Replace("{ReceiverEmail}", strReceiverEmail).Replace("{ReceiverName}", strReceiverName).Replace("{SenderEmail}", strSenderEmail).Replace("{SenderName}", strSenderName);

                // Set the format of the mail message body as HTML
                mail.IsBodyHtml = true;

                // Set the priority of the mail message to normal
                mail.Priority = MailPriority.Normal;

                // Send Email Using Network Credential
                using (var smtp = new SmtpClient())
                {
                    smtp.Host = ConfigurationManager.AppSettings["SMTP"];
                    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
            }
            catch(Exception)
            {
                //Response.Write(ex.Message);
            }
        }
    }
}